//
//  TXFEmotionKeyBoardView.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/7/4.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

class TXFEmotionKeyBoardView: UIView {
    
  override  init(frame: CGRect) {
        super.init(frame: frame)
    
    setupUI()
    }

  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }

    
    func setupUI(){
    
    
//    backgroundColor = RandomColor()
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
    addSubview(keyBoardToolBar)
        
    addSubview(emojiCollectionView)
        
    addSubview(pageControl)
        
     keyBoardToolBar.snp_makeConstraints { (make) -> Void in
        
        make.leading.trailing.bottom.equalTo(self)
        
        make.height.equalTo(37)
        }
       
        
    emojiCollectionView.snp_makeConstraints { (make) -> Void in
        
        
        make.top.leading.trailing.equalTo(self)
        
        
        make.bottom.equalTo(keyBoardToolBar.snp_top)
        }
        
        
        
    pageControl.snp_makeConstraints { (make) -> Void in
        
        
        make.bottom.equalTo(emojiCollectionView)
        
        make.centerX.equalTo(emojiCollectionView)
        
        
        }
        
        keyBoardToolBar.callbackClouse = { [weak self] (type: TXFKeyBoardToolBarType)->() in
        
            switch type {
            case.Default:
                
                print("Default")
            case.Emoji:
                
                print("Emoji")
            case.Lxh:
                
                print("Lxh")
            }
        
            
            //点击按钮之后 跳转到相对应的cell(组,row)
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            
            self?.emojiCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            
            
            self?.pageControl.numberOfPages = TXFEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
            
        self?.pageControl.currentPage = 0
            
            
            
            
            
            
            
     
        }
  
    }
    
    //MARK: 懒加载
    
    
    private lazy var keyBoardToolBar: TXFKeyBoardToolBar = TXFKeyBoardToolBar()
    
    private lazy var emojiCollectionView: TXFEmojiCollectionView = {
    let view = TXFEmojiCollectionView()
        
        view.bounces = false

        view.pagingEnabled = true

        
        view.delegate = self
        
        
        view.showsHorizontalScrollIndicator = false
    
        view.showsVerticalScrollIndicator = false
    
    
        return view
        
    }()
    
    //懒加载pageControl()
    
    private lazy var pageControl: UIPageControl = {
    
        let  view = UIPageControl()
        
        view.numberOfPages = TXFEmoticonTools.sharedTools.allEmoticons[0].count
        
        view.currentPage = 0
//通过KVC的方式为其设置背景图片
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
    
    return view
    }()
}

extension TXFEmotionKeyBoardView: UICollectionViewDelegate{


//代理方法:滚动时候
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        
        //滑动的时候按钮跳转状态:
        
        let  centerX = SCREENW/2 + scrollView.contentOffset.x
        //获取cell(当前)
        //排列顺序,原cell是无序的
        let cellArray =  emojiCollectionView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            
            return cell1.frame.origin.x < cell2.frame.origin.x
            
            
        }
        
        if cellArray.count == 2 {
        
        let firstCell  = cellArray.first!
            
         let lastCell = cellArray.last!
        
            
    var  indexPath:  NSIndexPath
        //判断我们的中心点是否在第一个cell中
    if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
            
         indexPath = emojiCollectionView.indexPathForCell(firstCell)!
        
            
            
            
    }else{
        
        
        indexPath = emojiCollectionView.indexPathForCell(lastCell)!
        
        
        
        
            }
        
            
            
            
            //按钮移动到相应的位置
            
            //        keyBoardToolBar.调用方法进行改变状态
            
            keyBoardToolBar.setCurrentButtons(indexPath.section)
            
            
            pageControl.numberOfPages = TXFEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
            
            
            
            pageControl.currentPage = indexPath.item
            
            
            
            
        
        }
        
        

        
    }









}
